package com.wenli.graduate2022.wenliGreenShop.repository.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenli.graduate2022.wenliGreenShop.entity.Product;
import com.wenli.graduate2022.wenliGreenShop.entity.ProductOrder;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HuangHai
 * @date 2021/10/13 14:49
 */
@Repository
public interface ProductOrderMapper {

    @Results(id = "productOrder",value={
            @Result(property = "productOrderAddress",column = "productorder_address",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.AddressMapper.findById",
                            fetchType = FetchType.LAZY)),
            @Result(property = "productOrderUser",column = "productorder_user_id",
                    one = @One(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.UserMapper.findById",
                            fetchType = FetchType.LAZY)),
            @Result(property = "productOrderItemList",column = "productorder_id",
                    many = @Many(select = "com.wenli.graduate2022.wenliGreenShop.repository.mapper.ProductOrderItemMapper.findByOrderId",
                            fetchType = FetchType.LAZY))//多对多查询，查询条件为订单id
    })
    @Select("select * from productOrder where productorder_user_id = #{id}")
    List<ProductOrder> findByUserId(Integer id);   //根据用户id查询它的所有订单

    @Options(useGeneratedKeys = true, keyProperty = "productorderId",keyColumn = "productorder_id")
    @Insert("insert into productOrder(productorder_code,productorder_address,productorder_detail_address,productorder_receiver," +
            "productorder_mobile,productorder_pay_date,productorder_status,productorder_user_id" +
            " values(#{productorderCode},#{address},#{productorderDetailAddress},#{productorderReceiver},#{productorderMobile}," +
            "#{productorderPayDate},#{productorderStatus}),#{userId})")
    @ResultMap("productOrder")
    int insert(ProductOrder productOrder,String address,String userId);

    @Select("select * from productOrder order by productorder_id desc")
    @ResultMap("productOrder")
    IPage<ProductOrder> findByPage(Page page);  //倒序分页查询

    @Select("select * from productOrder where productorder_code = #{code}")
    @ResultMap("productOrder")
    IPage<ProductOrder> findByCode(Page page,String code);       //根据订单号查询订单

    @Select("select * from productOrder where productorder_id = #{id}")
    @ResultMap("productOrder")
    ProductOrder findByOrderId(Integer id);        //根据产品订单id查询



    @Update("update productOrder set productorder_status = 1 where productorder_code = #{code}")
    @ResultMap("productOrder")
    int updateStatus(String code);   //商家发货，改变订单状态

    @Select("delete from productOrder where productorder_id = #{id}")
    @ResultMap("productOrder")
    int delete(Integer id);           //根据订单编号删除订单
}
